package com.lxt.controller.center;

import com.lxt.common.HttpResult;
import com.lxt.common.PagedGridResult;
import com.lxt.controller.BaseController;
import com.lxt.enums.YesOrNo;
import com.lxt.pojo.BO.center.OrderItemsCommentBO;
import com.lxt.pojo.OrderItems;
import com.lxt.pojo.Orders;
import com.lxt.service.center.MyCommentsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author kevin.lv
 * @date 2022/8/10 21:20
 */
@Api(value = "用户中心评价模块", tags = {"用户中心评价模块相关接口"})
@RestController
@RequestMapping("mycomments")
public class MyCommentsController extends BaseController {

    @Autowired
    private MyCommentsService myCommentsService;


    @ApiOperation(value = "查询订单列表", notes = "查询订单列表", httpMethod = "POST")
    @PostMapping("/pending")
    public HttpResult pending(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam String orderId) {

        //判断用户和订单是否关联
        HttpResult checkResult = checkUserOrder(orderId, userId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        //判断该订单是否已经评价过，评价过的不再继续评价
        Orders myOrder = (Orders) checkResult.getData();
        if (myOrder.getIsComment().equals(YesOrNo.YES.type)) {
            return HttpResult.errorMsg("该订单已评价! ");
        }

        List<OrderItems> list = myCommentsService.queryPendingComment(orderId);
        return HttpResult.ok(list);
    }

    @ApiOperation(value = "保存评论列表", notes = "保存评论列表", httpMethod = "POST")
    @PostMapping("/saveList")
    public HttpResult saveList(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "orderId", value = "订单Id", required = true)
            @RequestParam String orderId,
            @RequestBody List<OrderItemsCommentBO> commentList) {

        //判断用户和订单是否关联
        HttpResult checkResult = checkUserOrder(orderId, userId);
        if (checkResult.getStatus() != HttpStatus.OK.value()) {
            return checkResult;
        }

        //判断评论内容List不能为空
        if (commentList == null || commentList.isEmpty() || commentList.size() == 0) {
            return HttpResult.errorMsg("评论内容不能为空");
        }
        myCommentsService.saveComments(orderId, userId, commentList);
        return HttpResult.ok();
    }


    @ApiOperation(value = "查询我的评论", notes = "查询我的评论", httpMethod = "POST")
    @PostMapping("/query")
    public HttpResult query(
            @ApiParam(name = "userId", value = "用户Id", required = true)
            @RequestParam String userId,
            @ApiParam(name = "page", value = "当前页数", required = false)
            @RequestParam Integer page,
            @ApiParam(name = "pageSize", value = "分页每一页显示的条数", required = false)
            @RequestParam Integer pageSize) {

        if (StringUtils.isBlank(userId)) {
            return HttpResult.errorMsg(null);
        }
        if (page == null) {
            page = 1;
        }
        if (pageSize == null) {
            pageSize = COMMENT_PAGE_SIZE;
        }

        PagedGridResult pagedGridResult = myCommentsService.queryMyComments(userId, page, pageSize);
        return HttpResult.ok(pagedGridResult);
    }

}
